package com.dmai.capacity.platform.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dmai.capacity.platform.common.constant.CommonConstant;
import com.dmai.capacity.platform.common.model.*;
import com.dmai.capacity.platform.db.service.impl.ServiceImpl;
import com.dmai.capacity.platform.common.service.IUserBaseService;
import com.dmai.capacity.platform.user.mapper.OauthUserMapper;
import com.dmai.capacity.platform.user.mapper.SysRoleMenuMapper;
import com.dmai.capacity.platform.user.mapper.SysUserMapper;
import com.dmai.capacity.platform.user.service.ISysRoleUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service
public class UserBaseServiceImpl extends ServiceImpl<OauthUserMapper, OauthUser> implements IUserBaseService {

	@Autowired
	private PasswordEncoder passwordEncoder;

	@Resource
	private ISysRoleUserService roleUserService;

	@Resource
	private SysRoleMenuMapper roleMenuMapper;

	@Override
	public LoginAppUser findByUsername(String username) {
		OauthUser sysUser = this.selectByUsername(username);
		return getLoginAppUser(sysUser);
	}

	@Override
	public LoginAppUser findByOpenId(String username) {
		OauthUser sysUser = this.selectByOpenId(username);
		return getLoginAppUser(sysUser);
	}

	@Override
	public LoginAppUser findByMobile(String phone) {
		OauthUser sysUser = this.selectByMobile(phone);
		return getLoginAppUser(sysUser);
	}

	/**
	 * 根据用户名查询用户
	 * 
	 * @param username
	 * @return
	 */
	@Override
	public OauthUser selectByUsername(String username) {
		List<OauthUser> users = baseMapper.selectList(Wrappers.<OauthUser>lambdaQuery().eq(OauthUser::getUsername, username));
		return getUser(users);
	}

	/**
	 * 根据手机号查询用户
	 * 
	 * @param mobile
	 * @return
	 */
	public OauthUser selectByMobile(String mobile) {
		List<OauthUser> users = baseMapper.selectList(Wrappers.<OauthUser>lambdaQuery().eq(OauthUser::getMobile, mobile));
		return getUser(users);
	}

	/**
	 * 根据openId查询用户
	 * 
	 * @param openId
	 * @return
	 */
	public OauthUser selectByOpenId(String openId) {
		List<OauthUser> users = baseMapper.selectList(Wrappers.<OauthUser>lambdaQuery().eq(OauthUser::getOpenId, openId));
		return getUser(users);
	}

	/**
	 * 根据userId查询用户
	 * 
	 * @param userId
	 * @return
	 */
	@Override
	public LoginAppUser findByUserId(String userId) {

		OauthUser sysUser = baseMapper.selectById(userId);
		return getLoginAppUser(sysUser);
	}

	private OauthUser getUser(List<OauthUser> users) {
		OauthUser user = null;
		if (users != null && !users.isEmpty()) {
			user = users.get(0);
		}
		return user;
	}

	public LoginAppUser getLoginAppUser(OauthUser sysUser) {
		if (sysUser != null) {
			LoginAppUser loginAppUser = new LoginAppUser();
			BeanUtils.copyProperties(sysUser, loginAppUser);
			HashSet permissions = new HashSet();
			permissions.add("all");
			loginAppUser.setPermissions(permissions);
			return loginAppUser;
		}
		return null;
	}
}